package app.model;

import java.util.List;

import com.agile.jfinal.model.CoreModel;
import com.jfinal.ext.plugin.tablebind.TableBind;

/**
* 数据库表对应的Model类，继承了CoreModel
* @author qiutian 
* @date 2013-12-19
*/
@TableBind(tableName="app_menu",pkName="pk_menu") 
public class AppMenu extends CoreModel<AppMenu>{

	public static final AppMenu dao = new AppMenu();

	public static final String PK_MENU = "pk_menu";   //主键
	public static final String VCODE = "vcode";   //编码
	public static final String VNAME = "vname";   //名称
	public static final String MENU_SORT = "menu_sort";   //根据数字大小排序
	public static final String ISLEAF = "isleaf";   //是否末级 0-否 1-是
	public static final String MENU_URL = "menu_url";   //菜单链接
	public static final String DOWNLOAD_URL = "download_url";   //下载链接
	public static final String PK_PARENT = "pk_parent";   //父主键
	public static final String PK_BASEDOC = "pk_basedoc";   //app基本光里档案主键
	public static final String CREATE_BY = "create_by";   //制单人
	public static final String CREATE_TIME = "create_time";   //制单时间
	public static final String TS = "ts";   //时间戳
	public static final String DR = "dr";   //删除标记
	public static final String COLUMN_1 = "column_1";   //预留字段1
	public static final String COLUMN_2 = "column_2";   //预留字段2
	public static final String COLUMN_3 = "column_3";   //预留字段3
	public static final String COLUMN_4 = "column_4";   //预留字段4
	public static final String COLUMN_5 = "column_5";   //预留字段5


	/**数据库中的表名*/
	@Override
	public String getTableName() {
		// TODO Auto-generated method stub
		return "app_menu";
	}


	/**主键字段名称*/
	@Override
	public String getPrimaryKey() {
		// TODO Auto-generated method stub
		return PK_MENU;
	}

	/**
	 * 新增保存方法，非末级节点
	 * @return
	 */
	public boolean addAppMenuModel() {
		// TODO Auto-generated method stub
		
		 //设置父节点为非末级节点
		String pk_parent = get(PK_PARENT);
		AppMenu parentModel = findById(pk_parent);
		if(parentModel!=null) parentModel.set(ISLEAF, 0).update();
		this.set(ISLEAF, 1);  //设置当前节点为末级
		return save();
	}
	
	/**
	 * 删除数据，并查询当前节点是否有同级节点，如果没有，则父节点变为末级
	 */
	public boolean delAppMenuModel() {
		// TODO Auto-generated method stub
		
		//查询要删除的数据
		AppMenu appMenu = queryById();
		
		 //查询当前节点是否有同级节点
		String pk_parent = appMenu.get(PK_PARENT);
		List listModel = query(PK_PARENT, pk_parent);
		
		if(listModel==null || listModel.size()==0){ //如果没有同级，则父节点变为末级
			AppMenu parentModel = findById(pk_parent); 
			parentModel.set(ISLEAF, 1).update();
		}else{
			
		}
		return appMenu.delete();
	}

}
